/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package Common;

import Core.Configuration;
import java.io.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 *
 * @author harish
 */

public class Log
{
    private static String _filename= "Log.txt";
    private static File f=null;
    private static Log _instance =null;
    private Log(){}

    public static Log GetInstance()
    {
        if(_instance==null)
        {
            _instance=new Log();
        }
        return _instance;
    }
    
    public void Create()
    {
        try
        {
            CreateFile();
        }
        catch(IOException e)
        {
            try
            {
            Log.GetInstance().Write("Error in creating Log file" + e.getMessage());
            }
            catch(IOException i)
            {

            }
        }
    }
    
    private void CreateFile()throws IOException
    {
        try
        {            
            f=new File(Configuration.GetInstance().GetLogFilePath()+_filename);
            if(!f.exists())
            {
                f.createNewFile();
            }
        }
        catch(IOException e)
        {
            MessageBox.GetInstance().Show("Error in creating Log file" + e.getMessage());
        }

    }

    public void Write(String s) throws IOException
    {
        FileWriter fw=new FileWriter(f,true);
        BufferedWriter w=new BufferedWriter(fw);
        try
        {
            if(f.exists())
            {
                w.append(DateAndTime.GetDateTime()+ "\t\t\t\t\t\t\t" + s );
                w.newLine();
                fw.flush();
            }
            else
            {
                f.createNewFile();
                w.append(DateAndTime.GetDateTime()+ "\t\t\t\t\t\t\t" + s );
                w.newLine();
                fw.flush();
            }
        }
        catch(IOException e)
        {
            MessageBox.GetInstance().Show("Error in Writing to Log");
        }
        finally
        {
            w.close();
        }
    }
    
}
